package com.ctshk.rpc.order.scenic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.scenic.dto.ScenicOrderTouristDTO;
import com.ctshk.rpc.order.scenic.entity.ScenicOrder;
import com.ctshk.rpc.order.scenic.entity.ScenicOrderTourist;
import com.ctshk.rpc.order.scenic.mapper.ScenicOrderMapper;
import com.ctshk.rpc.order.scenic.mapper.ScenicOrderTouristMapper;
import com.ctshk.rpc.order.scenic.req.ScenicOrderTouristModifyReq;
import com.ctshk.rpc.order.scenic.req.ScenicOrderTouristReq;
import com.ctshk.rpc.order.scenic.service.IScenicOrderTouristService;
import com.ctshk.rpc.scenic.dto.ScenicProductTicketDetailDTO;
import com.ctshk.rpc.scenic.req.ScenicProductTicketDetailReq;
import com.ctshk.rpc.scenic.service.IScenicProductService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 门票订单旅客信息 服务实现类
 * </p>
 *
 * @author 张志朋
 * @since 2021-02-24
 */
@DubboService
public class ScenicOrderTouristServiceImpl extends ServiceImpl<ScenicOrderTouristMapper, ScenicOrderTourist> implements IScenicOrderTouristService {

    @Autowired
    private ScenicOrderTouristMapper scenicOrderTouristMapper;

    @Autowired
    private ScenicOrderMapper scenicOrderMapper;

    @DubboReference
    private IScenicProductService iScenicProductService;

    @Override
    public Result<List<ScenicOrderTouristDTO>> list(ScenicOrderTouristReq scenicOrderTouristReq,TokenUser tokenUser) {
        QueryWrapper<ScenicOrderTourist> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("order_id",scenicOrderTouristReq.getOrderId());
        List<ScenicOrderTourist> list = scenicOrderTouristMapper.selectList(wrapper);
        List<ScenicOrderTouristDTO> scenicOrderTouristDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (ScenicOrderTourist scenicOrderTourist : list) {
                ScenicOrderTouristDTO scenicOrderTouristDTO = EntityUtil.copy(scenicOrderTourist, ScenicOrderTouristDTO.class);
                ScenicProductTicketDetailReq scenicProductTicketDetailReq = new ScenicProductTicketDetailReq();
                if (null != scenicOrderTourist.getTicketsProductId()) {
                    scenicProductTicketDetailReq.setId(scenicOrderTourist.getTicketsProductId().toString());
                }
                Result<ScenicProductTicketDetailDTO> result = iScenicProductService.scenicProductTicketDetail(scenicProductTicketDetailReq);
                if (result.isSuccess() && result.getData() != null) {
                    ScenicProductTicketDetailDTO scenicProductTicketDetailDTO = result.getData();
                    scenicOrderTouristDTO.setStockNumber(scenicProductTicketDetailDTO.getStockNumber());
                    scenicOrderTouristDTO.setSupplierTicketNumber(scenicProductTicketDetailDTO.getSupplierTicketNumber());
                }
                QueryWrapper<ScenicOrder> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("is_deleted", 0);
                queryWrapper.eq("id", scenicOrderTourist.getOrderId());
                ScenicOrder scenicOrder = scenicOrderMapper.selectOne(queryWrapper);
                if (scenicOrder != null) {
                    scenicOrderTouristDTO.setCostPrice(scenicOrder.getSingleTicketFee());
                    if(scenicOrder.getProductTicketNumber()!=null) {
                        if (scenicOrder.getProductTicketNumber().intValue() > 0 && scenicOrder.getServiceFee()!=null) {
                            scenicOrderTouristDTO.setCharge(scenicOrder.getServiceFee().divide(new BigDecimal(scenicOrder.getProductTicketNumber().toString())));
                        }
                    }
                }
                scenicOrderTouristDTOList.add(scenicOrderTouristDTO);
            }
        }
        return Result.success(scenicOrderTouristDTOList);
    }

    @Override
    public Result modify(ScenicOrderTouristModifyReq scenicOrderTouristModifyReq, TokenUser tokenUser) {
        ScenicOrderTourist scenicOrderTourist = scenicOrderTouristMapper.selectById(scenicOrderTouristModifyReq.getId());
        if(scenicOrderTourist != null){
            scenicOrderTourist.setSurname(scenicOrderTouristModifyReq.getSurname());
            scenicOrderTourist.setName(scenicOrderTouristModifyReq.getName());
            scenicOrderTourist.setTouristType(scenicOrderTouristModifyReq.getTouristType());
            scenicOrderTourist.setBirthday(scenicOrderTouristModifyReq.getBirthday());
            scenicOrderTourist.setTouristType(scenicOrderTouristModifyReq.getTouristType());
            scenicOrderTourist.setCertificatesNumber(scenicOrderTouristModifyReq.getCertificatesNumber());
            scenicOrderTourist.setVaildDate(scenicOrderTouristModifyReq.getVaildDate());
            scenicOrderTourist.setCertificatesTypeId(Long.parseLong(scenicOrderTouristModifyReq.getCertificatesType().getValue()));
            scenicOrderTourist.setCertificatesType(scenicOrderTouristModifyReq.getCertificatesType().getLabel());
            scenicOrderTourist.setGmtModified(LocalDateTime.now());
            scenicOrderTourist.setModifiedId(tokenUser.getId());
            scenicOrderTouristMapper.updateById(scenicOrderTourist);
        }
        return Result.success(scenicOrderTourist.getId());
    }
}
